/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.maslyk.invitation.entitybean;

import java.math.BigDecimal;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.audit.PropertyLabel;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;
import com.maslyk.invitation.enums.OrderPositionTypeEnum;

/**
 *
 * @author kris
 */
@Entity
@Table(name = "product")
@ObjectName(label="Artykuł")
public class Product implements GenericEntity, IOrderPosition
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "code")
    @PropertyLabel(label="Kod")
    private String code;
    @Basic(optional = false)
    @Column(name = "totalCount")
    @PropertyLabel(label="Ilość")
    private int totalCount = 0;
    @Basic(optional = false)
    @Column(name = "unitPrice")
    @PropertyLabel(label="Cena")
    private Double unitPrice = new Double(0.00);
    @Column(name = "note")
    @PropertyLabel(label="Uwagi")
    private String note;
    @JoinColumn(name = "orderId", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity = Order.class)
    private Order order;

    public Product()
    {
    }

    public Product(Integer id)
    {
        this.id = id;
    }

    public Product(Integer id, String code, int totalCount, Order order, Double unitPrice)
    {
        this.id = id;
        this.code = code;
        this.totalCount = totalCount;
        this.order = order;
        this.unitPrice = unitPrice;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public int getTotalCount()
    {
        return totalCount;
    }

    public void setTotalCount(int totalCount)
    {
        this.totalCount = totalCount;
    }

    public Double getUnitPrice()
    {
        return unitPrice;
    }

    public void setUnitPrice(Double unitPrice)
    {
        this.unitPrice = unitPrice;
    }

    public String getNote()
    {
        return note;
    }

    public void setNote(String note)
    {
        this.note = note;
    }

    public Order getOrder()
    {
        return order;
    }

    public void setOrder(Order order)
    {
        this.order = order;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        if (!(object instanceof Product))
            return false;
        Product other = (Product) object;
     
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        return this.code;
    }

    public Double getAmount()
    {
        double totalAmount = 0.0d;

        totalAmount += totalCount * unitPrice.doubleValue();
        BigDecimal amount = new BigDecimal(totalAmount);
        amount.setScale(2,BigDecimal.ROUND_HALF_UP);
        return amount.doubleValue();
    }

    public OrderPositionTypeEnum getType()
    {
        return OrderPositionTypeEnum.PRODUCT;
    }

    public Double getUnitprice()
    {
        return unitPrice;
    }

	public String getKey() {
		return getOrder().getNumber();
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Product p = (Product) super.clone();
		p.setId(null);
		p.setOrder(null);
		return p;
	}
	
	public boolean isInserted() {
		return id != null;
	}
}
